package com.openflag.bwicservice.service.impl;

import com.openflag.bwicservice.controller.SellerController;
import com.openflag.bwicservice.domain.Bwic;
import com.openflag.bwicservice.domain.CancelRequest;
import com.openflag.bwicservice.domain.Result;
import com.openflag.bwicservice.domain.WarrantAgreeRequest;
import com.openflag.bwicservice.repository.BidRepository;
import com.openflag.bwicservice.repository.BwicRepository;
import com.openflag.bwicservice.service.SellerService;
import lombok.extern.java.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.time.chrono.ChronoLocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.openflag.bwicservice.domain.ResultStatus.*;
import static com.openflag.bwicservice.util.ResultUtil.*;
@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private BwicRepository bwicRepository;
    private Logger LOG = LoggerFactory.getLogger(SellerController.class);



    @Autowired
    private BidRepository bidRepository;

    @Override
    public String warrantAgreementCheck(WarrantAgreeRequest warrantAgreeRequest){
        Bwic queryBwic = new Bwic();
        queryBwic.setCusip(warrantAgreeRequest.getCusip());

        Optional<Bwic> optionalBwic = bwicRepository.findOne(Example.of(queryBwic));
        LOG.info("=====>{}.=====>{}",warrantAgreeRequest.getCreatedDate(),warrantAgreeRequest.getDueDate());
        if(optionalBwic.isPresent()){
            return "Cusip duplication";
        }

        if(warrantAgreeRequest.getDueDate()
                        .compareTo(warrantAgreeRequest.getCreatedDate().toLocalDate())<0){
            return "DueDate is before createdDate";
        }
        if(warrantAgreeRequest.getPosition() < 1){
            return "Position is not positive";
        }


        return "success";
    }


    @Override
    public List<Bwic> sellerBwicList(Long sellerId){
        List<Bwic> bwicList = new ArrayList<>();;
        bwicList =  bwicRepository.getSellerBwicBySellerId(sellerId);


        return bwicList;


    }

    @Override
    public Void sellerAddBwic(WarrantAgreeRequest warrantAgreeRequest){


        Bwic newBwic = new Bwic();
        newBwic.setCusip(warrantAgreeRequest.getCusip());
        newBwic.setPosition(warrantAgreeRequest.getPosition());
        newBwic.setPrice(0.00);
        newBwic.setMarketValue(0L);
        newBwic.setDueDate(warrantAgreeRequest.getDueDate());
        newBwic.setCreatedBy(warrantAgreeRequest.getClientName());
        newBwic.setCreatedDate(warrantAgreeRequest.getCreatedDate());
        newBwic.setCreatedById(warrantAgreeRequest.getCreatedById());
        newBwic.setStatus(0);
        bwicRepository.save(newBwic);
        return null;
    }

    @Override
    public Result<Bwic> sellerCancelBwic(CancelRequest cancelRequest){

        Optional<Bwic> optionalBwic = bwicRepository.getSellerBwicByBwicIdAndSellerId(cancelRequest.getBwicId(),cancelRequest.getClientId());

        if(!optionalBwic.isPresent()){
            return fail(BWIC_NOT_EXIST);
        }
        List<Integer> integerList= new ArrayList<>();
        integerList.add(-1);
        integerList.add(2);
        integerList.add(3);
        if (integerList.indexOf(optionalBwic.get().getStatus())!=-1){
            return fail(BWIC_WRONG_STATUS);
        }

        bwicRepository.delete(optionalBwic.get());


        return success(sellerBwicList(cancelRequest.getClientId()));


    }

}
